<template>
	<div>
		<el-dialog
			:title="isEdit ? '编辑' : '新增'"
			:visible.sync="dialogVisible"
			width="50%"
			:before-close="handleClose">
			<el-form label-position="left" :rules="formRules.resumeForm" ref="resumeForm" label-width="80px" :model="userData">
				<el-form-item v-if="isEdit" label="学院名称" prop="departmentname">
					<el-input v-model="userData.departmentname"/>
				</el-form-item>
        <el-form-item v-else label="学院名称" prop="departmentname">
					<el-input v-model="departmentname"/>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
    		<el-button type="primary" @click="onEditResumeForm" v-loading="loading">确 定</el-button>
  		</span>
		</el-dialog>
	</div>
</template>

<script>
  import * as college from '@/api/college';

  export default {
    name: 'addDialog',
		props: {
      dialogVisible: {
        type: Boolean,
				default: false
			},
      isEdit: {
        type: Boolean
			},
      userData: {
        type: Object,
				default: {}
      },
      departmentname: ''
		},
		data() {
      return {
        loading: false,
        resumeForm: {
          name: '',
          workExperience: '',
          skill: ''
        }
			}
		},
		methods: {
      async onEditResumeForm() {
        try {
          this.$refs['resumeForm'].validate(async valid => {
            if (valid) {
              this.loading = true;
              if (this.isEdit) {
                await college.editUser(this.userData);
                this.$notify({
                  title: '信息',
                  type: 'success',
                  message: '修改成功!'
                });
              } else {
                await college.addUser({ departmentname: this.departmentname });
                this.$notify({
                  title: '信息',
                  type: 'success',
                  message: '添加成功!'
                });
              }
              this.handleFresh();
              this.handleClose();
						}
					})
        } catch (e) {
					console.log(e);
        } finally {
					this.loading = false
        }
			},
      handleClose() {
        this.$emit('closeDialog');
			},
			handleFresh() {
        this.$emit('fresh');
			}
		},
		watch: {
      dialogVisible(newVal, oldVal) {
        if (newVal) {
          Object.assign(this.resumeForm, this.resumeData);
          console.log(this.resumeForm);
          if (!this.isEdit) {
            this.$nextTick(() => {
              this.$refs['resumeForm'].resetFields();
            })
					}
				}
			}
		},
		created () {
      this.resumeForm = {
        name: '',
        workExperience: '',
        skill: ''
      };
		}
  }
</script>

<style scoped>

</style>